package com.ruoyi.hr.controller.hr.welfare.welfarecalculationconfig;

import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.hr.domain.welfare.welfarecalculationconfig.CcdBeneItemType;
import com.ruoyi.hr.domain.welfare.welfarecalculationconfig.CcdBenefitbase;
import com.ruoyi.hr.domain.welfare.welfarecalculationconfig.CcdBenefititem;
import com.ruoyi.hr.domain.welfare.welfareregion.WelfareType;
import com.ruoyi.hr.service.welfare.welfareRegion.WelfareTypeService;
import com.ruoyi.hr.service.welfare.welfarecalculationconfig.ICcdBenefitbaseService;
import com.ruoyi.hr.service.welfare.welfarecalculationconfig.ICcdBenefititemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 福利计算项目Controller
 *
 * @author ruoyi
 * @date 2019-12-27
 */
@Controller
@RequestMapping("/welfare/benefititem")
public class CcdBenefititemController extends BaseController {
    private String prefix = "hr/welfare/welfarecalculationconfig/welfarecalculationpro";

    @Autowired
    private ICcdBenefititemService ccdBenefititemService;

    @Autowired
    private ICcdBenefitbaseService ccdBenefitbaseService;

    @Autowired
    private WelfareTypeService welfareTypeService;



    @RequiresPermissions("system:benefititem:view")
    @GetMapping()
    public String benefititem() {
        return prefix + "/benefititem";
    }

    /**
     * 查询福利计算项目列表
     */
    @RequiresPermissions("system:benefititem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CcdBenefititem ccdBenefititem) {
        PageDomain pageDomain = getPageDomain();
        int PageNum = pageDomain.getPageNum();
        int pageSize = pageDomain.getPageSize(); //取得显示数
        int PageCount;//总数据量
        String Beneitemsum = "";
        List<CcdBenefititem> list = ccdBenefititemService.selectCcdBenefititemList(ccdBenefititem, PageNum, pageSize);
        for (CcdBenefititem ccdBenefititem1 : list) {
            if (ccdBenefititem1.getBeneitemsum() != null) {
                ccdBenefititem1.setBeneitemsum(ccdBenefititemService.findBeneitemsum(ccdBenefititem1.getBeneitemsum()));
            }
        }
        PageCount = ccdBenefititemService.findCount(ccdBenefititem);
        return getDataTable1(list, PageCount);
    }

    /**
     * 导出福利计算项目列表
     */
    @RequiresPermissions("system:benefititem:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcdBenefititem ccdBenefititem) {
        /*
        List<CcdBenefititem> list = ccdBenefititemService.selectCcdBenefititemList(ccdBenefititem);
        ExcelUtil<CcdBenefititem> util = new ExcelUtil<CcdBenefititem>(CcdBenefititem.class);
        return util.exportExcel(list, "benefititem");
        */
        return null;
    }

    /**
     * 新增福利计算项目
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        //对应福利类型
        List<WelfareType> welfareTypeColumns = welfareTypeService.findWelfareTypes();
        mmap.put("welfareTypeColumns", welfareTypeColumns);
        //对应福利基数
        List<CcdBenefitbase> ccdBenefitbases = ccdBenefitbaseService.findAll();
        mmap.put("ccdBenefitbases", ccdBenefitbases);
        //计算类型
        List<CcdBeneItemType> ccdBeneItemTypes = ccdBenefitbaseService.findCcdBeneItemType();
        mmap.put("ccdBeneItemTypes", ccdBeneItemTypes);
        //对应汇总项目
        List<CcdBenefititem> ccdBenefititems =  ccdBenefititemService.findAll();
        mmap.put("ccdBenefititems", ccdBenefititems);
        return prefix + "/add";
    }

    /**
     * 新增保存福利计算项目
     */
    @RequiresPermissions("system:benefititem:add")
    @Log(title = "福利计算项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CcdBenefititem ccdBenefititem) {
        return toAjax(ccdBenefititemService.insertCcdBenefititem(ccdBenefititem));
    }

    /**
     * 修改福利计算项目
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CcdBenefititem ccdBenefititem = ccdBenefititemService.selectCcdBenefititemById(id);
        mmap.put("ccdBenefititem", ccdBenefititem);

        //对应福利类型
        List<WelfareType> welfareTypeColumns = welfareTypeService.findWelfareTypes();
        mmap.put("welfareTypeColumns", welfareTypeColumns);
        //对应福利基数
        List<CcdBenefitbase> ccdBenefitbases = ccdBenefitbaseService.findAll();
        mmap.put("ccdBenefitbases", ccdBenefitbases);
        //计算类型
        List<CcdBeneItemType> ccdBeneItemTypes = ccdBenefitbaseService.findCcdBeneItemType();
        mmap.put("ccdBeneItemTypes", ccdBeneItemTypes);
        //对应汇总项目
        List<CcdBenefititem> ccdBenefititems =  ccdBenefititemService.findAll();
        mmap.put("ccdBenefititems", ccdBenefititems);
        return prefix + "/edit";
    }

    /**
     * 修改保存福利计算项目
     */
    @RequiresPermissions("system:benefititem:edit")
    @Log(title = "福利计算项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CcdBenefititem ccdBenefititem) {
        return toAjax(ccdBenefititemService.updateCcdBenefititem(ccdBenefititem));
    }

    /**
     * 删除福利计算项目
     */
    @RequiresPermissions("system:benefititem:remove")
    @Log(title = "福利计算项目", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ccdBenefititemService.deleteCcdBenefititemByIds(ids));
    }
}
